package com.zhaofan.beanInfo;

import com.zhaofan.pojo.Persion;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyEditorSupport;
import java.util.stream.Stream;

/**
 * @Author: zhaofan
 * @Date: 2021/8/19
 * @Description:java bean的原信息
 */
public class BeanInfoDemo {
    public static void main(String[] args) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(Persion.class);
        Stream.of(beanInfo.getPropertyDescriptors()).forEach(property->{
            System.out.println(property);
            //PropertyDescriptor 允许添加属性编辑器
            Class<?> propertyType = property.getPropertyType();
            String propertyName = property.getName();
            if("age".equals(propertyName)){
                property.setPropertyEditorClass(StringToIntegerPropertyEditor.class);
//                property.createPropertyEditor();
            }
        });
    }

    static class StringToIntegerPropertyEditor extends PropertyEditorSupport{

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            Integer value=Integer.valueOf(text);
            setValue(value);
        }
    }
}

